<template>
  <div v-loading="statusLoading" class="px-2 pt-2">
    <attach-upload
      :tags="uploadTags"
      :batch-no="relatedId"
      :view-mode="isView"
      v-bind="$listeners"
      ref="upload"
      @after-data="handleAfterData"
    ></attach-upload>
  </div>
</template>

<script>
import AttachUpload from '@/bank/components/attach/Upload'

export default {
  components: {
    AttachUpload,
  },
  props: {
    viewMode: Boolean,
    relatedId: String,
    bankId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      loading: false,
      statusLoading: false,
      projectData: {},
      signData: {},
      attachs: [],
    }
  },
  computed: {
    isCompany() {
      return +this.projectData.customer_type === 1
    },
    uploadTags() {
      return [{ parentTag: '调查附件', tag: '联保评审会纪要', required: false }]
    },
    isView() {
      return this.viewMode
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    handleAfterData({ list, emit }) {
      if (emit) {
        this.$emit('success', list)
      }
      if (this.isView) return
      this.attachs = list
      this.updateStatus()
    },
    updateStatus() {
      // 判断附件状态
      const listObj = this.attachs.reduce((prev, curr) => {
        const tag = curr.attachment_type
        if (!prev[tag]) prev[tag] = []
        prev[tag].push(curr)
        return prev
      }, {})
      const isAllUpload = this.uploadTags.every(tag => {
        if (!tag.required) return true
        const atts = listObj[tag.tag]
        return atts && atts.length > 0
      })
      const currentStatus = isAllUpload ? 1 : 0
      this.$emit('status', currentStatus)
    },
  },
}
</script>

<style lang="scss" scoped>
.attach-title {
  font-size: 1.1rem;
  font-weight: bold;
}
</style>
